/**
 * Express.js Cookie示例
 */

//导入Express模块
const express = require("express");
//导入Cookie模块
const cookieParser = require("cookie-parser")
//创建Express应用
const app = express();
//Express http服务端口
const port = 3001;

app.use(express.json());
app.use(express.urlencoded({extended:false}));

app.use(cookieParser());

app.get("/",function(req,res){
    if(req.cookies.remember){
        res.send(`记住了。<a href="/forget">忘记</a>`);
    }else{
        let str =`
            <form method="post">
                <label>
                    <input type="checkbox" name="remember" />记住了。
                </label>
                <input type="submit" value = "提交"/>
            </form>` 
            ;
            res.send(str);
    }
});

app.get("/forget",function(req,res){
    res.clearCookie("remember");
    res.redirect("back");
});

app.post("/",function(req,res){
    let minute=6000;
    if(req.body.remember)
        res.cookie("remember",1,{maxAge:minute});
    res.redirect("back");
});

//启动Express服务
app.listen(port,function(){
    console.log(`MyApp访问地址：http://127.0.0.1:${port}`);
});